﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using QingzimaScenario;
using QingzimaLogicModel;

namespace QingzimaWebClient.Controllers
{
    public class HoutaiShuiController : Controller
    {
        //
        // GET: /HoutaiShui/

        public ActionResult List()
        {
            ViewBag.ShuiList = ShuiScenario.HuoquModels();
            return View();
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(ShuiLM ShuiLM, HttpPostedFileBase file)
        {
            if (string.IsNullOrEmpty(ShuiLM.Biaoti))
            {
                ViewBag.State = "请输入标题";
            }
            else
            {
                ShuiLM.Neirong = Request.Form.Get("HtmlTextArea");

                if (file != null)
                {
                    string path = System.Web.HttpContext.Current.Server.MapPath("/UploadImg");
                    string name = System.IO.Path.GetFileName(file.FileName);
                    string ext = name.Substring(name.LastIndexOf(".") + 1);
                    string time = DateTime.Now.ToString("yyyy-MM-dd-HHmmss-ffff");
                    if (ext == "jpg" || ext == "gif" || ext == "png" || ext == "jpeg" || ext == "bmp")
                    {
                        string newname = time + '.' + ext;
                        file.SaveAs(System.IO.Path.Combine(path, newname));
                        ShuiLM.Tupian = "UploadImg/" + newname;
                        ShuiScenario.Tianjia(ShuiLM);
                        return RedirectToAction("Add");
                    }
                    else
                    {
                        Response.Write("<script language='javascript'>alert('图片格式不正确')</script>");
                    }
                }
                else
                {
                    Response.Write("<script language='javascript'>alert('请上传图片')</script>");                
                }            
            }
            return View();
        }

        public ActionResult Update(string key)
        {
            ShuiLM ShuiLM = ShuiScenario.HuoquByKey(key);
            ViewBag.Neirong = ShuiLM.Neirong;
            return View(ShuiLM);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Update(ShuiLM ShuiLM, HttpPostedFileBase file)
        {
            if (string.IsNullOrEmpty(ShuiLM.Biaoti))
            {
                ViewBag.State = "请输入标题";
            }
            else
            {
                ShuiLM.Neirong = Request.Form.Get("HtmlTextArea");

                if (file != null)
                {
                    string path = System.Web.HttpContext.Current.Server.MapPath("/UploadImg");
                    string name = System.IO.Path.GetFileName(file.FileName);
                    string ext = name.Substring(name.LastIndexOf(".") + 1);
                    string time = DateTime.Now.ToString("yyyy-MM-dd-HHmmss-ffff");
                    if (ext == "jpg" || ext == "gif" || ext == "png" || ext == "jpeg" || ext == "bmp")
                    {
                        string newname = time + '.' + ext;
                        file.SaveAs(System.IO.Path.Combine(path, newname));
                        ShuiLM.Tupian = "UploadImg/" + newname;
                        ShuiScenario.Tianjia(ShuiLM);
                        return RedirectToAction("List");
                    }
                    else
                    {
                        Response.Write("<script language='javascript'>alert('图片格式不正确')</script>");
                    }
                }
                else
                {
                    ShuiScenario.Tianjia(ShuiLM);
                    return RedirectToAction("List");
                }
            }
            return View();
        }

        public ActionResult Del(string key)
        {
            ShuiScenario.Shanchu(key);
            return RedirectToAction("List");
        }
    }
}
